package com.example.wanln.util;
import java.util.TreeMap;

/**
 * 
 * 万里牛api类
 *
 */
public class WanlnApi {
	
	/**
	 * 订单推送接口 
	 * @param tradesArray 数据
	 * @return
	 */
	public static String wanlnApiTradesOpen(String tradesArray) {
		TreeMap<String, Object> systemParamMap = new TreeMap<>();
		systemParamMap.put("app_key", WanlnConfig.APP_KEY);
		systemParamMap.put("format", "json");
		systemParamMap.put("trades", tradesArray);
		systemParamMap.put("timestamp", String.valueOf(System.currentTimeMillis()));
		String tmpParameter = WanlnUtil.buildParameter(systemParamMap);
		StringBuilder sign = new StringBuilder();
		sign.append(WanlnConfig.APP_SECRET);
		sign.append(tmpParameter);
		sign.append(WanlnConfig.APP_SECRET);
		systemParamMap.put("sign", WanlnUtil.md5(sign.toString()).toUpperCase());
		String urlStr=WanlnConfig.TEST_WANLN_URL+WanlnConfig.VERSION+WanlnConfig.TRADES_API;		
		return WanlnUtil.httpConnection("POST",urlStr, systemParamMap);
	}
	
	/**
	 * 获取订单状态接口 
	 * @param tradeIds 订单号（多个订单以半角逗号相隔，如”123,456”，最多支持200个订单号）
	 * @return
	 */
	public static String wanlnApiTradesErpStatus(String tradeIds) {
		TreeMap<String, Object> systemParamMap = new TreeMap<>();
		systemParamMap.put("app_key", WanlnConfig.APP_KEY);
		systemParamMap.put("format", "json");
		systemParamMap.put("timestamp", String.valueOf(System.currentTimeMillis()));
		systemParamMap.put("shop_type", 100);
		systemParamMap.put("shop_nick", WanlnConfig.SHOP_NICK);
		systemParamMap.put("trade_ids", tradeIds);
		String tmpParameter = WanlnUtil.buildParameter(systemParamMap);
		StringBuilder sign = new StringBuilder();
		sign.append(WanlnConfig.APP_SECRET);
		sign.append(tmpParameter);
		sign.append(WanlnConfig.APP_SECRET);
		systemParamMap.put("sign", WanlnUtil.md5(sign.toString()).toUpperCase());
		String urlStr=WanlnConfig.TEST_WANLN_URL+WanlnConfig.VERSION+WanlnConfig.TRADES_ERP_STATES_API;		
		return WanlnUtil.httpConnection("GET",urlStr, systemParamMap);
	}
	
	/**
	 * 商品类目推送接口 
	 * @param categoriesArray 数据
	 * @return
	 */
	public static String wanlnApiCategoriesOpen(String categoriesArray) {
		TreeMap<String, Object> systemParamMap = new TreeMap<>();
		systemParamMap.put("app_key", WanlnConfig.APP_KEY);
		systemParamMap.put("format", "json");
		systemParamMap.put("categories", categoriesArray);
		systemParamMap.put("timestamp", String.valueOf(System.currentTimeMillis()));
		String tmpParameter = WanlnUtil.buildParameter(systemParamMap);
		StringBuilder sign = new StringBuilder();
		sign.append(WanlnConfig.APP_SECRET);
		sign.append(tmpParameter);
		sign.append(WanlnConfig.APP_SECRET);
		systemParamMap.put("sign", WanlnUtil.md5(sign.toString()).toUpperCase());
		String urlStr=WanlnConfig.TEST_WANLN_URL+WanlnConfig.VERSION+WanlnConfig.CATEGORY_API;		
		return WanlnUtil.httpConnection("POST",urlStr, systemParamMap);
	}
	
	/**
	 * 商品推送接口 
	 * @param itemsArray 数据
	 * @return
	 */
	public static String wanlnApiItemsOpen(String itemsArray) {
		TreeMap<String, Object> systemParamMap = new TreeMap<>();
		systemParamMap.put("app_key", WanlnConfig.APP_KEY);
		systemParamMap.put("format", "json");
		systemParamMap.put("items", itemsArray);
		systemParamMap.put("timestamp", String.valueOf(System.currentTimeMillis()));
		String tmpParameter = WanlnUtil.buildParameter(systemParamMap);
		StringBuilder sign = new StringBuilder();
		sign.append(WanlnConfig.APP_SECRET);
		sign.append(tmpParameter);
		sign.append(WanlnConfig.APP_SECRET);
		systemParamMap.put("sign", WanlnUtil.md5(sign.toString()).toUpperCase());
		String urlStr=WanlnConfig.TEST_WANLN_URL+ WanlnConfig.VERSION+WanlnConfig.ITEM_API;		
		return WanlnUtil.httpConnection("POST",urlStr, systemParamMap);
	}
	
	/**
	 * 单笔查询库存接口 
	 * @param itemId 商品编号
	 * @param skuId 对应商品推送的中skuID
	 * @return
	 */
	public static String wanlnApiInventoriesErpSingle(String itemId, String skuId) {
		TreeMap<String, Object> systemParamMap = new TreeMap<>();
		systemParamMap.put("app_key", WanlnConfig.APP_KEY);
		systemParamMap.put("format", "json");
		systemParamMap.put("timestamp", String.valueOf(System.currentTimeMillis()));
		systemParamMap.put("shop_type", 100);
		systemParamMap.put("shop_nick", WanlnConfig.SHOP_NICK);
		systemParamMap.put("item_id", itemId);
		systemParamMap.put("sku_id", skuId);
		String tmpParameter = WanlnUtil.buildParameter(systemParamMap);
		StringBuilder sign = new StringBuilder();
		sign.append(WanlnConfig.APP_SECRET);
		sign.append(tmpParameter);
		sign.append(WanlnConfig.APP_SECRET);
		systemParamMap.put("sign", WanlnUtil.md5(sign.toString()).toUpperCase());
		String urlStr=WanlnConfig.TEST_WANLN_URL+WanlnConfig.VERSION+WanlnConfig.INVENTORIES_ERP_SINGLE_API;		
		return WanlnUtil.httpConnection("GET",urlStr, systemParamMap);
	}
	
	/**
	 * 批量查询库存接口
	 * @param page 当前页
	 * @param limit 每页条数
	 * @param start 开始时间
	 * @param end 结束时间
	 * @return
	 */
	public static String wanlnApiInventoriesErp(int page, int limit,String start,String end) {
		TreeMap<String, Object> systemParamMap = new TreeMap<>();
		systemParamMap.put("app_key", WanlnConfig.APP_KEY);
		systemParamMap.put("format", "json");
		systemParamMap.put("timestamp", String.valueOf(System.currentTimeMillis()));
		systemParamMap.put("page", page);
		systemParamMap.put("limit", limit);
		systemParamMap.put("start", start);
		systemParamMap.put("end", end);
		String tmpParameter = WanlnUtil.buildParameter(systemParamMap);
		StringBuilder sign = new StringBuilder();
		sign.append(WanlnConfig.APP_SECRET);
		sign.append(tmpParameter);
		sign.append(WanlnConfig.APP_SECRET);
		systemParamMap.put("sign", WanlnUtil.md5(sign.toString()).toUpperCase());
		String urlStr=WanlnConfig.TEST_WANLN_URL+WanlnConfig.VERSION+WanlnConfig.INVENTORIES_ERP_API;		
		return WanlnUtil.httpConnection("GET",urlStr, systemParamMap);
	}
	
	private WanlnApi() {
	}
}
